<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { FormInstance, Message } from '@arco-design/web-vue';
  import useLoading from '@/hooks/loading';
  import Editor from '@/components/editor/index.vue';
  import { getGeoProvice, getgeocity } from '@/api/policy';
  import { getpolicydeplist, getpolicyIndex } from '@/api/dataCenter';
  import { useUserStore } from '@/store';
  import { addcompany } from '@/api/enterlibery';
  const formRef = ref<FormInstance | null>(null);
  const props = defineProps({
    visible: Boolean,
    params: Object,
  });
  const userStore = useUserStore();
  const provice = ref([]);
  const city = ref([]);
  const { loading, setLoading } = useLoading(false);
  const isShow = ref(true);
  defineOptions({
    name: 'addcomponenItem',
  });
  const formType = ref();
  provice.value = userStore.allProvice;

  onMounted(() => {
    formType.value = [
      {
        name: 'gsmc',
        label: '企业名称',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: true,
            message: '请输入',
          },
        ],
      },
      {
        name: 'cymc',
        label: '曾用名',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请选择',
          },
        ],
      },
      {
        name: 'ywmc',
        label: '英文名称',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'qylx',
        label: '企业类型',
        type: 'a-input-tag',
        fieldProps: {
          span: 24,
          placeholder: '',
        },
        rules: [
          {
            required: false,
            message: '请选择',
          },
        ],
      },
      {
        name: 'djjg',
        label: '登记机关',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'gswz',
        label: '官方网址',
        type: 'a-input',
        fieldProps: {
          span: 24,
          placeholder: '例：https://kjj.luzhou.gov.cn/tzgg/content_1074796',
        },
        rules: [
          {
            required: false,
            message: '请选择',
          },
        ],
      },
      {
        name: 'gsyx',
        label: '邮箱',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'jyzt',
        label: '经营状态',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'frdb',
        label: '法人代表',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'yyqx',
        label: '营业期限',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'hzrq',
        label: '核准日期',
        type: 'a-date-picker',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'hy',
        label: '行业',
        type: 'a-input-tag',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'tyshxydm',
        label: '统一社会信用代码',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'nsrsbh',
        label: '纳税人识别号',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'zzjgdm',
        label: '组织机构代码',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'gszch',
        label: '工商注册号',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'zczb',
        label: '注册资本',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'sjzb',
        label: '实缴资本',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'nsrzz',
        label: '纳税人资质',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'rygm',
        label: '人员规模',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'zcdz',
        label: '注册地址',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'jyfw',
        label: '经营范围',
        type: 'a-input',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'gsjj',
        label: '公司简介',
        type: 'a-textarea',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: false,
            message: '请输入',
          },
        ],
      },
      {
        name: 'clrq',
        label: '成立日期',
        type: 'a-date-picker',
        fieldProps: {
          span: 24,
        },
        rules: [
          {
            required: true,
            message: '请选择',
          },
        ],
      },
      {
        name: 'province_id',
        label: '省',
        type: 'a-select',
        fieldProps: {
          span: 8,
          allowSearch: true,
          fieldNames: { value: 'id', label: 'name' },
          placeholder: '请选择',
        },
        selectOptions: provice.value,
        rules: [
          {
            message: '请选择',
            required: true,
          },
        ],
      },
      {
        name: 'city_id',
        label: '市',
        type: 'a-tree-select',
        fieldProps: {
          span: 8,
          allowSearch: true,
          fieldNames: { value: 'id', label: 'name' },
          placeholder: '请选择',
        },
        selectOptions: [],
        rules: [
          {
            message: '请选择',
            required: true,
          },
        ],
      },
    ];
  });
  const formData = ref({});
  const emit = defineEmits(['update:visible', 'refresh']);
  const handleSubmit = async () => {
    if (!(await formRef.value?.checkForm())) {
      try {
        setLoading(true);
        const dataForm = formRef.value?.formModel;
        const res = await addcompany({
          ...dataForm,
        });
        setLoading(false);
        Message.success(res.msg);
        emit('update:visible', false);
        emit('refresh');
      } catch (error) {
        // Message.error(error);
        setLoading(false);
      } finally {
        setLoading(false);
      }
    } else {
      Message.error('请正确填写表单！');
    }
  };

  const content = ref('');
  // 市
  watch(
    () => formRef.value?.formModel.province_id,
    (newVal) => {
      getgeocity({
        province_id: newVal,
      }).then((res) => {
        formRef.value?.setFormItm('city_id', '');
        formType.value = [
          {
            name: 'gsmc',
            label: '企业名称',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: true,
                message: '请输入',
              },
            ],
          },
          {
            name: 'cymc',
            label: '曾用名',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请选择',
              },
            ],
          },
          {
            name: 'ywmc',
            label: '英文名称',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'djjg',
            label: '登记机关',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'qylx',
            label: '企业类型',
            type: 'a-input-tag',
            fieldProps: {
              span: 24,
              placeholder: '',
            },
            rules: [
              {
                required: false,
                message: '请选择',
              },
            ],
          },
          {
            name: 'gswz',
            label: '官方网址',
            type: 'a-input',
            fieldProps: {
              span: 24,
              placeholder: '例：https://kjj.luzhou.gov.cn/tzgg/content_1074796',
            },
            rules: [
              {
                required: false,
                message: '请选择',
              },
            ],
          },
          {
            name: 'gsyx',
            label: '邮箱',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'jyzt',
            label: '经营状态',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'frdb',
            label: '法人代表',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'yyqx',
            label: '营业期限',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'hzrq',
            label: '核准日期',
            type: 'a-date-picker',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'hy',
            label: '行业',
            type: 'a-input-tag',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'tyshxydm',
            label: '统一社会信用代码',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'nsrsbh',
            label: '纳税人识别号',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'zzjgdm',
            label: '组织机构代码',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'gszch',
            label: '工商注册号',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'zczb',
            label: '注册资本',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'sjzb',
            label: '实缴资本',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'nsrzz',
            label: '纳税人资质',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'rygm',
            label: '人员规模',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'zcdz',
            label: '注册地址',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'jyfw',
            label: '经营范围',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'gsjj',
            label: '公司简介',
            type: 'a-textarea',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请输入',
              },
            ],
          },
          {
            name: 'clrq',
            label: '成立日期',
            type: 'a-date-picker',
            fieldProps: {
              span: 24,
            },
            rules: [
              {
                required: false,
                message: '请选择',
              },
            ],
          },
          {
            name: 'province_id',
            label: '省',
            type: 'a-select',
            fieldProps: {
              span: 8,
              allowSearch: false,
              fieldNames: { value: 'id', label: 'name' },
              placeholder: '请选择',
            },
            selectOptions: provice.value,
            rules: [
              {
                message: '请选择',
                required: false,
              },
            ],
          },
          {
            name: 'city_id',
            label: '市',
            type: 'a-select',
            fieldProps: {
              span: 8,
              allowSearch: true,
              fieldNames: { value: 'id', label: 'name' },
              placeholder: '请选择',
            },
            selectOptions: res.data,
            rules: [
              {
                message: '请选择',
                required: false,
              },
            ],
          },
        ];
      });
    }
  );
</script>

<template>
  <a-modal
    title="添加公司"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    :ok-loading="loading"
    width="95%"
    @cancel="
      () => {
        emit('update:visible', false);
      }
    "
  >
    <pro-form
      ref="formRef"
      hiddenBtn
      :form-json="formType"
      :form-data="formData"
    ></pro-form>
  </a-modal>
</template>

<style scoped lang="less">
  :deep(.arco-select-view-inner) {
    text-align: left !important;
  }
  .titile {
    margin-left: 12px;
    font-size: 13px;
    color: #4e5969;
  }
</style>
